package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.model.TbSpecification;
import com.dongdongshop.model.TbSpecificationOption;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.BrandVO;
import com.dongdongshop.vo.TSOVO;
import com.dongdongshop.vo.TSVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("Specification")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;

    @PostMapping("getAll")
    public Result brandList(@RequestBody TSVO tsvo) {
        List<TSVO> list =  specificationService.findBrand(tsvo);
        return Result.OK().setData(list);
    }


    //规格回显
    @RequestMapping("getSpecById")
    public Result getSpecById(@RequestParam Long id){
        TSVO tsvo = specificationService.getSpecById(id);
        return Result.OK().setData(tsvo);
    }


    //增加规格表
    @PostMapping("addSpecification")
    public Result addSpecification(TSVO tsvo,String optionList){
        List<TbSpecificationOption> list = JSONObject.parseArray(optionList, TbSpecificationOption.class);
        specificationService.add(tsvo,list);
        return Result.OK();
    }

    @PutMapping("update")
    public Result updateSpecification(@RequestBody TSVO specVO) {
        specificationService.update(specVO);
        return Result.OK();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除商品品牌")
    public Result DeleteTSVO(@RequestParam Long[] ids){
        specificationService.DeleteTSVO(ids);
        return Result.OK();
    }

    @PostMapping("list")
    public Result list(){
        List<TbSpecification> list = specificationService.list();
        return Result.OK().setData(list);
    }
}
